﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Dimok.Web.UI.JS.MSAjaxExtentions.js" assembly="Dimok.Web.UI" />
(function($J) {
  $J.extend({ bubbleTipSettings: { pause: 2500, topFly: 100, cols: 60, flashCss: { color: "darkgreen", fontWeight: "bold"}} });
  $J.extend({ bubbleTip: function(settings) {
    if (settings.error) settings = $J.extend({ "mailTo": true }, settings);
    var mailTo = settings.mailTo ?
    "<a href='mailto" +
      ":help@afchome.net?subject=Cry for help&body='>Mail for help</a>\
     " : "";
    $J("<div class='bubbleTip' title='Click to close this one,\nAlt-Click to close all,\nCtrl-Click to copy to clipboard.' ><h3></h3><div class='body'></div><div class='mailTo'>" + mailTo + "</div></div>")
    .appendTo(document.body)
    .draggable({
      start: function(e) {
        $J(this).stop(true);
        this.inDrag = true;
      }
    })
    .bubbleTip(settings);
  }
  });
  $J.fn.extend({
    bubbleTip: function(settings) {
      ///	<summary>
      ///		Shows a tooltip for specified amount of time
      ///	</summary>
      ///	<returns type="jQuery" />
      ///	<param name="settings" type="Object">
      ///		{pos:{left,top},
      ///		 element:if(!pos)pos = element.top/left,
      ///		 pause:2500,
      ///		 topFly:100,
      ///		 stay:false,
      ///		 cols:60,
      ///		 html:"Text to show",
      ///		 callback:Function,
      ///		 destroy:false(when done)}
      ///    flash: (boolean)/if(flash)stay=true/,
      ///    flashCss: { color: darkgreen, fontWight: bold}
      ///	</param>

      if (this.length == 0) { return $J.bubbleTip(settings); }
      var s = $J.extend({}, $J.bubbleTipSettings, settings);
      var pos = s.pos;
      if (!pos) {
        var $el = $J($J(":first", s.element)[0] || $J(s.element)[0]);
        if (!$el[0]) throw "bubbleTip: Either [pos] or [element] must by present in settings.";
        pos = $el.position(); //  { top: $el.getTopPos(), left: $el.getLeftPos() };
        var height = $el.outerHeight();
        this[0].style.visibility = "hidden";
        this[0].style.display = "block";
        var thisHeight = Sys.UI.DomElement.getBounds(this[0]).height;
        pos.top = pos.top + height - thisHeight;
        s = $J.extend({ pos: pos }, s);
        this[0].style.visibility = "visible";
        this[0].style.display = "none";
      }
      var reNL = new RegExp(".{" + s.cols + "}", "g");
      var message = (s.html || "") + (s.error || "")
        .replace(/\s?The transaction ended in the trigger[.]\s*The batch has been aborted[.]/, "");
      if (s.mailTo) {
        var $mt = $J(".mailTo a", this);
        $mt.attr("href", $mt.attr("href").replace("body=", "body=" + escape(message)));
      }
      if (s.error) {
        s = $J.extend({ stay: true, destroy: false }, s);
        s.css = $J.extend({ color: "crimson" }, s.css || {});
      }
      if (s.flash) s = $J.extend({ stay: true }, s);
      this.css(s.css || {});
      return this.each(function() {
        var $this = $J(".body", this).html(Dimok.FormatPopUpMessage(message, s.cols))
          .css(s.flash ? s.flashCss : {})
          .parent().css({ left: s.pos.left, top: s.pos.top })
          .stop()
					.fadeIn(1000)
					.animate({ top: "+=" + (thisHeight || 0) }, { duration: height ? 1000 : 0, queue: false })
					.click(function(e) {
					  $J(this).stop(true);
					  var exit = false;
					  if (!s.stay) exit = s.stay = true;
					  if (this.inDrag) exit = !(this.inDrag = false);
					  if (exit) return;
					  if (event.altKey) { $J(".bubbleTip").unbind().remove(); $J(".bubble").hide(); }
					  if (!event.ctrlKey) { s.destroy ? $J(this).unbind().remove() : $J(this).hide(); if (s.callback) s.callback(); }
					  else clipboardData.setData('Text', $J(".body", this).text());
					});
        if (!s.stay) {
          $this.animate({ left: "+=0" }, s.pause || 2500,
					         function() {
					           if (s.callback) s.callback();
					           $J(this).animate({ top: "-=" + Math.min(s.topFly, $J(this).position().top - 2) }, { duration: 2000, queue: false });
					         }
					).fadeOut(2000);
          if (s.destroy) $this.queue(function() { $J(this).dequeue(); $J(this).unbind().remove(); });
        }
      });
    },
    getLeftPos: function() {
      var element = this[0];
      if (!element || !element.currentStyle) return 0;
      for (var n = 0, e = element; e; e = e.offsetParent) {
        debugger;
        n += e.offsetLeft + parseInt(e.currentStyle.paddingLeft) + (parseInt(e.currentStyle.marginLeft) || 0);
      }
      return n + $J(e).getLeftPos();
    },
    getTopPos: function() {
      var element = this[0];
      if (!element || !element.currentStyle) return 0;
      for (var n = 0, e = element; e; e = e.offsetParent)
        n += e.offsetTop - (e.cellPadding || 0) * 2 - (e.cellSpacing || 0) * 2 + parseInt(e.currentStyle.paddingTop) + (parseInt(e.currentStyle.marginTop) || 0);
      return n + $J(e).getTopPos();
    },
    clearSelect: function() {
      return this.each(function() {
        if (this.tagName == 'SELECT')
          this.options.length = 0;
      });
    },
    fillSelect: function(data) {
      return this.clearSelect().each(function() {
        if (this.tagName == 'SELECT') {
          var dropdownList = this;
          $J.each(data, function(index, optionData) {
            var option = new Option(optionData.t, optionData.v);
            if ($J.browser.msie) dropdownList.add(option); else dropdownList.add(option, null);
          });
        }
      });
    }
  });
})(jQuery);